using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;

namespace RedPointSystem
{
    [CustomEditor(typeof(RedPoint))]
    public class RedPointInspector : Editor
    {
        private RedPoint m_target = null;

        //是否为编辑模式，若为编辑模式则可以调整红点位置
        private bool m_isEditor = false;

        //当前选中的RedPointData的索引
        private long m_selectIndex = 0;

        private void OnDisable()
        {
            /*if (this.m_target != null)
            {
                m_target.DrawIcon(false, true);
            }*/
        }

        public override void OnInspectorGUI()
        {
            m_target = target as RedPoint;
            if (m_target.m_RedPointList == null)
            {
                m_target.m_RedPointList = new List<RedPointData>();
            }

            EditorGUIUtility.labelWidth = 80;

            EditorGUILayout.BeginVertical();
            m_isEditor = EditorGUILayout.Toggle("编辑模式:", m_isEditor);

            if (!Application.isPlaying)
            {
                EditorGUILayout.BeginHorizontal();
                
                if (GUILayout.Button("添加"))
                {
                    RedPointData addRd = new RedPointData();
                    addRd.index = System.DateTime.Now.Ticks + Random.Range(1, 20);
                    m_target.m_RedPointList.Add(addRd);
                }

                EditorGUILayout.EndHorizontal();
            }

            if (m_isEditor)
            {
                EditorGUILayout.HelpBox("从上到下判定第一个为true的Checker进行显示", MessageType.Warning);
                EditorGUILayout.Space();
            }

            if (Application.isPlaying && !m_isEditor)
            {
                EditorGUILayout.EndVertical();
                return;
            }

            if (!m_isEditor)
            {
                m_target.DrawIcon(false, true);
            }

            for (int i = 0; i < m_target.m_RedPointList.Count; i++)
            {
                RedPointData rd = m_target.m_RedPointList[i];
                EditorGUILayout.BeginHorizontal();
                bool isFlodout = UnityEditorHelper.EditorHelper.DrawHeader(rd.ToString(), rd.index == m_selectIndex);
                if (isFlodout && GUILayout.Button("收起"))
                {
                    m_selectIndex = 0;
                    isFlodout = false;
                }

                if (i > 0 && GUILayout.Button("上移"))
                {
                    RedPointData tempRd = m_target.m_RedPointList[i - 1];
                    m_target.m_RedPointList[i - 1] = rd;
                    m_target.m_RedPointList[i] = tempRd;
                    break;
                }

                if (i < m_target.m_RedPointList.Count - 1 && GUILayout.Button("下移"))
                {
                    RedPointData tempRd = m_target.m_RedPointList[i + 1];
                    m_target.m_RedPointList[i + 1] = rd;
                    m_target.m_RedPointList[i] = tempRd;
                    break;
                }

                if (!Application.isPlaying)
                {
                    if (GUILayout.Button("删除"))
                    {
                        m_target.m_RedPointList.RemoveAt(i);
                        break;
                    }
                }

                EditorGUILayout.EndHorizontal();
                if (isFlodout)
                {
                    m_target.DrawIcon(m_isEditor, true, rd);
                    UnityEditorHelper.EditorHelper.BeginContents();
                    ERedPointStyle tempStyel = (ERedPointStyle) EditorGUILayout.EnumPopup("外观", rd.style);
                    if (m_isEditor && (tempStyel != rd.style || m_selectIndex != rd.index)) //不一样，则先删掉
                    {
                        m_target.DrawIcon(false, true, rd);
                    }

                    rd.style = tempStyel;
                    rd.localPos.x = EditorGUILayout.IntSlider("x", (int) rd.localPos.x, -500, 500);
                    rd.localPos.y = EditorGUILayout.IntSlider("y", (int) rd.localPos.y, -500, 500);
                    rd.redPointTypeId = EditorGUILayout.IntField("类型Id", rd.redPointTypeId);
                    rd.remindContion = EditorGUILayout.IntField("额外参数", rd.remindContion);

                    GUILayout.Space(6f);
                    m_selectIndex = rd.index;
                    
                    UnityEditorHelper.EditorHelper.EndContents();
                }
            }
            
            EditorGUILayout.Space();
            if (GUILayout.Button("保存"))
            {
                m_isEditor = false;
                m_target.DrawIcon(false, true);
                        
                EditorUtility.SetDirty(target);
            }

            EditorGUILayout.EndVertical();
        }
    }
}

